/**
 * Created by Administrator on 2017/10/10.
 */
var Page={
    ajax:function (method, url, data, successFn, failFn) {
        //layer模块加载完成后执行ajax请求
        $.ajax({
            url: url,
            data: data,
            type: method,
            dataType: "json",
            beforeSend: function () {
                layer.load();
            },
            success: function (data) {
                if (successFn) {
                    successFn(data);
                } else {
                    layer.msg(data.successMessage); // 默认
                }
            },
            error: function (error) {
                layer.alert('服务器忙，请您稍后再试！', function () {
                    layer.closeAll('loading');
                });
            },
            complete: function () {
                layer.closeAll('loading');
            }
        });

    },
    postAjax:function (url, data, successFn, failFn) {
        Page.ajax('post',url, data, successFn, failFn);
    }
};
var Dialog={
    /**
     * 打开一个弹出窗口
     * @param title 必须。窗口名称
     * @param content 必须。窗口内容。任意的文本或html或dom元素
     * @param options 可选。其他layer参数
     */
    open:function (title, content, options) {
        options = $.extend({
            type: 1,
            shadeClose: false,
            title: title,
            content: content
        }, options);

        layer.open(options);
    },
    /**
     * 打开一个弹出窗口
     * @param title 必须。窗口名称
     * @param url 必须。URL，需返回HTML内容，如果返回的是HTML片段，请使用openByUrl方法
     * @param options 可选。其他layer参数
     */
    openUrl:function (title, url, options) {
        options = $.extend({
            type: 2,
            shadeClose: false,
            title: title,
            content: url
        }, options);

        layer.open(options);
    },
    /**
     * 警告框
     * @param message 必须。提示信息
     * @param okFn 可选。点击确定的回调函数
     * @param options 可选。其他layer参数
     */
    alert: function (message, okFn, options) {
        options = $.extend({
            icon: 2,
            title: '系统提示',
            end: function(){
                layer.closeAll('loading');
            } //弹出层销毁
        }, options);

        layer.alert(message, options, function (index) {
            if (okFn) {
                okFn();
            }
            layer.close(index);
        });
    },
    /**
     * 关闭Frame
     * @param frameName window的name
     */
    closeFrame: function (frameName) {
        var index = parent.layer.getFrameIndex(frameName); //获取窗口索引
        parent.layer.close(index);
    },
};
var zTree={
    init:function (treeId,url,options) {
        options=$.extend({
            postData:{},
            onClick:function (event, treeId, treeNode, clickFlag) {
                
            },
            onDblClick:function (event, treeId, treeNode) {

            },
            check: {
                enable: true
            },
            view: {
                showLine: false,
                selectedMulti: false,
                nameIsHTML: true
            },
            data: {
                key: {
                    name: "text"
                },
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pid",
                    rootPId: "0"
                }
            }
        },options);
        var setting={
            check: options.check,
            view: options.view,
            data: options.data,
            callback:{
                onclick:function (event, treeId, treeNode, clickFlag) {
                    options.onClick(event, treeId, treeNode, clickFlag);
                },
                onDblClick:function (event, treeId, treeNode) {
                    options.onDblClick(event, treeId, treeNode)
                }
            }
        };
        if(typeof(url)=='string'){
            Page.postAjax(
                url,
                options.postData,
                function (data) {
                    $.fn.zTree.init($("#" + treeId), setting, data.result);
            },
                function (data) {

            })
        }
    }
}